# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.
#
# from AUR - adapted to you style
#
# Old Contributor: Matthias Fulz < mfulz [at] olznet [dot] de >

pkgname=openvas-scanner
pkgver=23.17.0
pkgrel=1
pkgdesc='OpenVAS Scanner of the Greenbone Community Edition.'
arch=('x86_64' 'aarch64')
url='https://github.com/greenbone/openvas-scanner'
license=('GPL-2.0-only')
depends=('gvm-libs' 'json-glib' 'libbsd' 'libgcrypt' 'redis' 'net-snmp' 'rsync')
makedepends=('cmake' 'doxygen' 'cargo' 'inetutils' 'graphviz')
optdepends=('greenbone-feed-sync: scripts for downloading updated feed informations')
install='openvas.install'
options=(!lto)
source=("https://github.com/greenbone/openvas-scanner/archive/v${pkgver}.tar.gz"
        "$pkgname.tmpfiles"
        'openvasd.service')
sha512sums=('e594bc9ef98c15359f408b784dcbac1ee9ac454c80a771e0f64ef5df27bcefbae19a8bb63332ff269e2f7da09265d373cc56b6c92652b81e96961fdaa8d46658'
            'c87d7890698368a6b8d569d9af73712676d0e0061de8e058cc6c7f609da1def99ebbf9b11d35d1dd0234538dbb07987ff18bbf186928ea39c33441f231eb8751'
            '476e0aaeb916959b93082bf2529774ff46db46e6e699c34ea0ac246c461d554c87d15c25a2bc6565c06f0cd39df5dd14609d99777608c1c8afb824b0a6de99ae')

prepare() {
  cd "$pkgname-$pkgver"

  sed -i '/-Werror/d' CMakeLists.txt

  cd rust
  export RUSTUP_TOOLCHAIN=stable
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cmake \
  -B build \
  -S "$pkgname-$pkgver" \
  -DCMAKE_BUILD_TYPE=None \
  -DCMAKE_INSTALL_PREFIX=/usr \
  -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
  -DSBINDIR=/usr/bin \
  -DLIBDIR=/usr/lib \
  -DSYSCONFDIR=/etc \
  -DLOCALSTATEDIR=/var \
  -DOPENVAS_DATA_DIR=/var/lib/openvas \
  -DOPENVAS_FEED_LOCK_PATH=/var/lib/openvas/feed-update.lock \
  -DOPENVAS_RUN_DIR=/run/ospd \
  -DOPENVAS_NVT_DIR=/var/lib/openvas/plugins \
  -DBUILD_WITH_NETSNMP=True

  make -C build

  # Build openvasd and scannerctl
  cd "$pkgname-$pkgver/rust"
  export RUSTUP_TOOLCHAIN=stable
  export CARGO_TARGET_DIR=target
  cargo build --frozen --release --all-features
}

package() {
  make DESTDIR="$pkgdir" -C build install

  find "$pkgname-$pkgver/rust/target/release" \
    -maxdepth 1 \
    -executable \
    -type f \
    -exec install -Dm 755 -t "$pkgdir/usr/bin/" {} +

  install -Dm 644 "$pkgname.tmpfiles" "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"
  install -Dm 644 openvasd.service -t "$pkgdir/usr/lib/systemd/system/"
  install -Dm 644 "$pkgname-$pkgver/config/redis-openvas.conf" \
    -t "$pkgdir/etc/redis/"
}

